#include"dlinklist.h"
#include<stdio.h>
#include<stdlib.h>

//初始化链表
dlinklist init_dlinklist(){
    dlinklist head = malloc(sizeof(dlinknode));
    if(head == NULL)return NULL;
    head->next = head;
    head->prev = head;
    return head;
}
//创建新节点
dlinklist new_node(int data){
    dlinklist new = malloc(sizeof(dlinknode));
    if(new == NULL)return NULL;
    new->data = data;
    new->next = new;
    new->prev = new;
    return new;
}
//链接新节点
void list_add_tail(dlinklist head, dlinklist new_node){
    if(head == NULL || new_node == NULL)return;
    new_node->prev = head->prev;
    new_node->next = head;

    head->prev->next = new_node;
    head->prev = new_node;
    return;
}
//剔除节点
void list_del(dlinklist node){
    node->prev->next = node->next;
    node->next->prev = node->prev;

    node->next = NULL;
    node->prev = NULL;
}
//奇偶重排
void reverst(dlinklist head){
    if(head == NULL)return;
    dlinklist pos = head->prev;
    while(pos->data % 2 == 0)pos = pos->prev;
    dlinklist p = pos;
    while (pos != head){
        if(pos->data % 2 == 0){
            p = pos->next;
            list_del(pos);
            list_add_tail(head, pos);
        }
        else p = pos;
        pos = p->prev;
    }
    return;
}
//遍历链表
void list_for_each(dlinklist head){
    if(head->next == head)return;
    dlinklist pos = head->next;
    while(pos != head){
        printf("%d ", pos->data);
        pos = pos->next;
    }
    printf("\n");
    return;
}